<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

<!--        <blockquote class="layui-elem-quote layui-text">-->
<!--            欢迎各位业主提出宝贵的意见 <a href="/doc/base/faq.html#form" target="_blank">为什么select/checkbox/radio没显示？</a> 2. <a href="/doc/modules/form.html#render" target="_blank">动态添加的表单元素如何更新？</a>-->
<!--        </blockquote>-->

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>个人中心</legend>
        </fieldset>

        <form class="layui-form" action="">
            <div class="layuimini-container">
                <div class="layuimini-main">
                    <div class="layui-form layuimini-form" lay-filter="editUser">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" name="username" readonly  lay-verify="required"
                                       lay-reqtext="管理账号不能为空" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-block">
                                <input type="text" name="address" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label required">手机</label>
                            <div class="layui-input-block">
                                <input type="text" name="tel" lay-verify="required" lay-reqtext="手机不能为空" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">地址</label>
                            <div class="layui-input-block">
                                <input type="text" name="address" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'miniTab', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            $ = layui.jquery;

        // 从session域中获取用户的id
        let id = ${owner.id};

        // 根据用户ID  查询用户信息   将查询用户信息进行数据的回显   from.value()   ?
        $.get('/user?method=selectById',{id},function (res) {
            if(res.code==0){
                var dt = res.data;
                console.log(dt)
                //表单数据回显
                form.val("editUser",dt)
                //重新渲染表单  避免表单控件无法显示
                form.render();
            }else {
                layer.msg(res.msg,{icon:2})
            }
        },'JSON')
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // 获取表单中的数据  转换成json字符串 便于数据的传递
            var user = JSON.stringify(data.field);
            console.log(user)

            // 发送ajax请求 修改用户信息
            $.post('/owner?method=update', {user}, function (res) {
                // 修改成功   重新加载数据表格
                if (res.code == 200) {
                    layer.msg(res.msg, function () {
                        // 关闭tab
                        miniTab.deleteCurrentByIframe();
                    })
                } else {
                    // 修改失败
                    layer.msg(res.msg, {icon: 2})
                }
                return false;
            });
        });
    });
</script>

</body>
</html>